package utils
{
	import flash.data.SQLConnection;
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.data.SQLTransactionLockType;
	import flash.events.EventDispatcher;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	import flash.filesystem.File;
	import flash.net.Responder;
	import flash.system.Capabilities;
	
	import model.Modellocator;
	
	import mx.collections.ArrayCollection;
	import mx.core.FlexGlobals;
	
	public class DatabaseClass extends EventDispatcher
	{
		public static var _instance:DatabaseClass;
		
		private var sqlConnection:SQLConnection;
		private var createTableSQLStmt:SQLStatement;
		private var insertRecordSQLStmt:SQLStatement;
		private var selectRecordSQLStmt:SQLStatement;
		private var updateRecordSQLStmt:SQLStatement;
		private var deleteRecordSQLStmt:SQLStatement;
		
		private var databaseFile:File;
		private var callBackFunc:Function;
		
		public var resultData:ArrayCollection;
		
		public function DatabaseClass(enforcer:SingletonEnforcer){
			super();
		}
		
		public static function getInstance():DatabaseClass{
			if(_instance == null){
				_instance = new DatabaseClass(new SingletonEnforcer());
			}
			return _instance;
		} 
		
		
		public function openDataBaseConnection():void{
			databaseFile = File.applicationStorageDirectory.resolvePath("r_retail_v0.db");
			sqlConnection = new SQLConnection();
			sqlConnection.open(databaseFile);
			createLoginMasterTable();
			createSKUMasterTable();
			createBillEntryTable();
			createBillSKUTable();
			//createCustomerMasterTable();
			//createRateMasterTable();
			//createBillNoTable();
			//createBillEntryDetailsTable();
			//createPaymentEntryTable();
		}
		
		private function createSKUMasterTable():void{
			var sqlQueryString:String= "create TABLE IF NOT EXISTS  skuMaster(itemName Text, itemCode Text, barcode Text, price Text, weight Text, quantity Text, quantityType Text, category Text);";
			executeCreateTableQuery(sqlQueryString);
		}
		private function createLoginMasterTable():void{
			var sqlQueryString:String= "create TABLE IF NOT EXISTS  loginMaster(loginName Text, loginPass Text, userType Text,  mobile Text);";
			executeCreateTableQuery(sqlQueryString);
		}
		
		private function createCustomerMasterTable():void{
			var sqlQueryString:String= "create TABLE IF NOT EXISTS  cutomerMaster (" +
				"custID Text, 	custName Text, 	custPhone 	Text, pantLength 	Text, pantW  		Text, pantS 		Text, pantB 	Text, pantNL 	Text, " +
				"pantNR Text,	pantT 	Text, 	pantF 		Text, pantBelt 		Text, pantFP 		Text, pantBP 		Text, pantWP 	Text,  " +
				"pantups Text,	pantF1 Text,	pantB1 		Text," +
				"shirtL Text, shirtSH 	Text, shirtSL 		Text, shirtSR 		Text, shirtAL 		Text, shirtC 	Text, shirtW Text," +
				"shirtS Text, shirtNeck Text, shirtPock 	Text, shirtCollar 	Text);";
			executeCreateTableQuery(sqlQueryString);
		}
		
		private function createRateMasterTable():void{
			var sqlQueryString:String= "create TABLE IF NOT EXISTS  rateMaster (rateCode Text, rateDescr Text, amount Text);";
			executeCreateTableQuery(sqlQueryString);
		}
		private function createBillSKUTable():void{
			var sqlQueryString:String= "create TABLE IF NOT EXISTS  billSKUTable(billNo INTEGER, itemName Text, itemCode INTEGER, barcode INTEGER, price INTEGER, total INTEGER, weight Text, quantity Text, category Text, itemStatus Text);";
			executeCreateTableQuery(sqlQueryString);
		}

		/*private function createBillEntryTable():void{
			var sqlQueryString:String= "create TABLE IF NOT EXISTS  BillEntry (billNo Text, billDate Numeric,  deliveryDate Numeric, " +
				"advanceAmount Numeric, balAmount Numeric,totAmount Numeric, descr Text,custID Text, custName Text);";
			executeCreateTableQuery(sqlQueryString);
		}*/
		
		private function createBillEntryDetailsTable():void{
			var sqlQueryString:String= "create TABLE IF NOT EXISTS  BillEntryDetails (billNo Text, qty Numeric, rateAmount Numeric," +
				"rateDescr Text, delivered  Text  );";
			executeCreateTableQuery(sqlQueryString);
		}
		
		private function createPaymentEntryTable():void{
			var sqlQueryString:String= "create TABLE IF NOT EXISTS  BillPaymentDetails (billNo Text, recdDate Numeric, recdAmount Numeric," +
				"custID Text, balAmount Numeric,totAmount Numeric );";
			executeCreateTableQuery(sqlQueryString);
			
		}
		private function createBillEntryTable():void{
			var sqlQueryString:String= "create TABLE IF NOT EXISTS  BillEntryTable (billNo INTEGER PRIMARY KEY AUTOINCREMENT, userName Text, timeStampFirst Text, total INTEGER, cash INTEGER, card INTEGER, coupon INTEGER, discount INTEGER, timeStampLast Text, billStatus Text);";
			executeCreateTableQuery(sqlQueryString);			
		}
		
		private function  executeCreateTableQuery(sqlQueryString:String):void{	
			createTableSQLStmt = new SQLStatement();
			createTableSQLStmt.sqlConnection = 	sqlConnection;
			createTableSQLStmt.text = sqlQueryString;			    			
			createTableSQLStmt.addEventListener(SQLErrorEvent.ERROR,createTableSqlQueryErrorHandler);
			createTableSQLStmt.addEventListener(SQLEvent.RESULT,createTableSqlQueryResultHandler);
			createTableSQLStmt.execute();	
		}
		
		private function createTableSqlQueryErrorHandler(e:SQLErrorEvent):void{
			createTableSQLStmt.removeEventListener(SQLErrorEvent.ERROR,createTableSqlQueryErrorHandler);
			createTableSQLStmt.removeEventListener(SQLEvent.RESULT,createTableSqlQueryResultHandler);
		}
		
		private function createTableSqlQueryResultHandler(e:SQLEvent):void{
			createTableSQLStmt.removeEventListener(SQLErrorEvent.ERROR,createTableSqlQueryErrorHandler);
			createTableSQLStmt.removeEventListener(SQLEvent.RESULT,createTableSqlQueryResultHandler);
		}
		
		public function insertIntoLoginMaster(data:Object):void{
			var sql:String ="INSERT INTO loginMaster " +
				"(loginName,loginPass, userType, mobile)" +
				" VALUES (:col1, :col2, :col3, :col4)";
			insertRecordSQLStmt = new SQLStatement();
			insertRecordSQLStmt.sqlConnection = sqlConnection;
			insertRecordSQLStmt.text = sql;
			insertRecordSQLStmt.parameters[':col1'] = data.loginName;
			insertRecordSQLStmt.parameters[':col2'] = data.loginPass;
			insertRecordSQLStmt.parameters[':col3'] = data.userType;
			insertRecordSQLStmt.parameters[':col4'] = data.mobile;
			insertRecordSQLStmt.execute();
		}
		
		public function insertIntoBillEntryTable(data:Object, callBack:Function = null):void{
			callBackFunc = callBack;
			var sql:String ="INSERT INTO BillEntryTable " +
				"(userName, timeStampFirst, total, cash, card, coupon, discount, timeStampLast, billStatus)" +
				" VALUES (:col1, :col2, :col3, :col4, :col5, :col6, :col7, :col8, :col9)";
			
			insertRecordSQLStmt = new SQLStatement();
			insertRecordSQLStmt.sqlConnection = sqlConnection;
			insertRecordSQLStmt.text = sql;
			insertRecordSQLStmt.parameters[':col1'] = data.userName;
			insertRecordSQLStmt.parameters[':col2'] = data.timeStampFirst;
			insertRecordSQLStmt.parameters[':col3'] = data.total;
			insertRecordSQLStmt.parameters[':col4'] = data.cashPaid;
			insertRecordSQLStmt.parameters[':col5'] = data.cardPaid;
			insertRecordSQLStmt.parameters[':col6'] = data.couponPaid;
			insertRecordSQLStmt.parameters[':col7'] = data.discount;
			insertRecordSQLStmt.parameters[':col8'] = data.timeStampLast;
			insertRecordSQLStmt.parameters[':col9'] = data.billStatus;
			insertRecordSQLStmt.execute(-1, new Responder( insertIntoBillEntryTableResultHandler, updateRecordErrorHandler ));
		}
		private var ML:Modellocator = Modellocator.getInstance();
		public function insertIntoBillSKUEntryTable(data:Object, callBack:Function = null):void{
			callBackFunc = callBack;
			
			var sql:String ="INSERT INTO billSKUTable " +
				"(billNo, itemName, itemCode, barcode, price, total, weight, quantity, category, itemStatus)" +
				" VALUES (:col1, :col2, :col3, :col4, :col5, :col6, :col7, :col8, :col9, :col10)";
			
			insertRecordSQLStmt = new SQLStatement();
			insertRecordSQLStmt.sqlConnection = sqlConnection;
			insertRecordSQLStmt.text = sql;
			insertRecordSQLStmt.parameters[':col1'] = ML.BILL_NO;
			insertRecordSQLStmt.parameters[':col2'] = data.name;
			insertRecordSQLStmt.parameters[':col3'] = Number(data.itemCode);
			insertRecordSQLStmt.parameters[':col4'] = Number(data.barcode);
			insertRecordSQLStmt.parameters[':col5'] = Number(data.price);
			insertRecordSQLStmt.parameters[':col6'] = Number(data.total);
			insertRecordSQLStmt.parameters[':col7'] = data.weight;
			insertRecordSQLStmt.parameters[':col8'] = data.quantity;
			insertRecordSQLStmt.parameters[':col9'] = data.category;
			insertRecordSQLStmt.parameters[':col10'] = data.itemStatus;
			
			insertRecordSQLStmt.execute(-1, new Responder( insertIntoBillSKUEntryTableResultHandler, updateRecordErrorHandler ));
		}
		
		private function insertIntoBillSKUEntryTableResultHandler(res:SQLResult):void{
			trace("Bill Generated");
			var arr:ArrayCollection ;//= new Array();
			if ( res != null && res.data != null ) {
				arr = new ArrayCollection(res.data);
			}
			if(callBackFunc != null){
				callBackFunc(arr);
			}
		}
		
		private function insertIntoBillEntryTableResultHandler(res:SQLResult):void{
			trace("Bill Generated");
			insertRecordSQLStmt.clearParameters();
			insertRecordSQLStmt = null;
			var arr:ArrayCollection ;//= new Array();
			if ( res != null && res.data != null ) {
				arr = new ArrayCollection(res.data);
			}
			if(callBackFunc != null){
				callBackFunc(arr);
			}
		}
		
		private function updateRecordErrorHandler(e:SQLErrorEvent):void{
			trace(e);
		}
		public function updateBillEntryTable(data:Object):void{
			var sqlQueryString:String = "update BillEntryTable set total ='" + data.total + "', cash='" + data.cashPaid +"', card='" + data.cardPaid +"', coupon='" + data.couponPaid +"', discount='" + data.discount +"' where billNo ='" + data.billNo +"'";
			updateRecordSQLStmt = new SQLStatement();
			updateRecordSQLStmt.sqlConnection = sqlConnection;
			updateRecordSQLStmt.text = sqlQueryString;
			updateRecordSQLStmt.execute(-1, new Responder( updateRecordResultHandler, selectRecordErrorHandler ) );
		}
		
		public function insertIntoSKUMaster(data:Object):void{
			var sql:String ="INSERT INTO skuMaster " +
				"(itemName, itemCode, barcode, price, weight, quantity, quantityType, category)" +
				" VALUES (:col1, :col2, :col3, :col4, :col5, :col6, :col7, :col8)";
			
			insertRecordSQLStmt = new SQLStatement();
			insertRecordSQLStmt.sqlConnection = sqlConnection;
			insertRecordSQLStmt.text = sql;
			insertRecordSQLStmt.parameters[':col1'] = data.itemName;
			insertRecordSQLStmt.parameters[':col2'] = data.itemCode;
			insertRecordSQLStmt.parameters[':col3'] = data.barcode;
			insertRecordSQLStmt.parameters[':col4'] = data.price;
			insertRecordSQLStmt.parameters[':col5'] = data.weight;
			insertRecordSQLStmt.parameters[':col6'] = data.quantity;
			insertRecordSQLStmt.parameters[':col7'] = data.quantityType;
			insertRecordSQLStmt.parameters[':col8'] = data.category;
			insertRecordSQLStmt.execute();
		}
		
		public function insterIntoCustomerMaster(data:Object):void{
			
			var sql:String ="INSERT INTO cutomerMaster " +
				"( custID,	custName,	custPhone, pantLength, pantW, pantS, pantB, pantNL, pantNR, pantT, pantF, pantBelt, pantFP, pantBP, pantWP, pantups, pantF1, pantB1," +
				" shirtL, shirtSH, shirtSL, shirtSR, shirtAL, shirtC, shirtW, shirtS, shirtNeck, shirtPock, shirtCollar )" +
				" VALUES (:col1, :col2, :col3, :col4, :col5, :col6, :col7, :col8, :col9, :col10, :col11, :col12, :col13, :col14, :col15, " +
				" :col16, :col17, :col18, :col19, :col20, :col21, :col22, :col23, :col24, :col25, :col26, :col27, :col28, :col29 )";
			
			insertRecordSQLStmt = new SQLStatement();
			insertRecordSQLStmt.sqlConnection = sqlConnection;
			insertRecordSQLStmt.text = sql;
			insertRecordSQLStmt.parameters[':col1'] = data.custID;
			insertRecordSQLStmt.parameters[':col2'] = data.custName;
			insertRecordSQLStmt.parameters[':col3'] = data.custPhone;
			insertRecordSQLStmt.parameters[':col4'] = data.pantLength;
			insertRecordSQLStmt.parameters[':col5'] = data.pantW;
			insertRecordSQLStmt.parameters[':col6'] = data.pantS;
			insertRecordSQLStmt.parameters[':col7'] = data.pantB;
			insertRecordSQLStmt.parameters[':col8'] = data.pantNL;
			insertRecordSQLStmt.parameters[':col9'] = data.pantNR;
			insertRecordSQLStmt.parameters[':col10'] = data.pantT;
			insertRecordSQLStmt.parameters[':col11'] = data.pantF;
			insertRecordSQLStmt.parameters[':col12'] = data.pantBelt;
			insertRecordSQLStmt.parameters[':col13'] = data.pantFP;
			insertRecordSQLStmt.parameters[':col14'] = data.pantBP;
			insertRecordSQLStmt.parameters[':col15'] = data.pantWP;
			insertRecordSQLStmt.parameters[':col16'] = data.pantups;
			insertRecordSQLStmt.parameters[':col17'] = data.pantF1;
			insertRecordSQLStmt.parameters[':col18'] = data.pantB1;
			insertRecordSQLStmt.parameters[':col19'] = data.shirtL;
			insertRecordSQLStmt.parameters[':col20'] = data.shirtSH;
			insertRecordSQLStmt.parameters[':col21'] = data.shirtSL;
			insertRecordSQLStmt.parameters[':col22'] = data.shirtSR;
			insertRecordSQLStmt.parameters[':col23'] = data.shirtAL;
			insertRecordSQLStmt.parameters[':col24'] = data.shirtC;
			insertRecordSQLStmt.parameters[':col25'] = data.shirtW;
			insertRecordSQLStmt.parameters[':col26'] = data.shirtS;
			insertRecordSQLStmt.parameters[':col27'] = data.shirtNeck;
			insertRecordSQLStmt.parameters[':col28'] = data.shirtPock;
			insertRecordSQLStmt.parameters[':col29'] = data.shirtCollar;
			
			insertRecordSQLStmt.execute();
		}
		
		public function insertIntoRateMaster(data:Object):void{

			var sql:String ="INSERT INTO rateMaster " +
				"(rateCode,rateDescr, amount)" +
				" VALUES (:col1, :col2, :col3)";
			
			insertRecordSQLStmt = new SQLStatement();
			insertRecordSQLStmt.sqlConnection = sqlConnection;
			insertRecordSQLStmt.text = sql;
			insertRecordSQLStmt.parameters[':col1'] = data.rateCode;
			insertRecordSQLStmt.parameters[':col2'] = data.rateDescr;
			insertRecordSQLStmt.parameters[':col3'] = data.amount;
			insertRecordSQLStmt.execute();
		}
		
		
		public function insertIntoIntoBillEntry(data:Object):void{
			var sql:String ="INSERT INTO BillEntry " +
				"(billNo,billDate, deliveryDate, advanceAmount, balAmount,totAmount,descr,custID, custName)" +
				" VALUES (:col1, :col2, :col3, :col4, :col5, :col6, :col7, :col8, :col9)";
			insertRecordSQLStmt = new SQLStatement();
			insertRecordSQLStmt.sqlConnection = sqlConnection;
			insertRecordSQLStmt.text = sql;
			insertRecordSQLStmt.parameters[':col1'] = data.billNo;
			insertRecordSQLStmt.parameters[':col2'] = data.billDate;
			insertRecordSQLStmt.parameters[':col3'] = data.deliveryDate;
			insertRecordSQLStmt.parameters[':col4'] = data.advanceAmount;
			insertRecordSQLStmt.parameters[':col5'] = data.balAmount;
			insertRecordSQLStmt.parameters[':col6'] = data.totAmount;
			insertRecordSQLStmt.parameters[':col7'] = data.descr;
			insertRecordSQLStmt.parameters[':col8'] = data.custID;
			insertRecordSQLStmt.parameters[':col9'] = data.custName;
			insertRecordSQLStmt.execute();
		}
		
		public function insertIntoBillEntryDetails(billEntryDetails:ArrayCollection) : void{
			var newQuery:Boolean  = true
			var query:String = '';
			var iCnt:int = 0
			for (var i:int =0; i<billEntryDetails.length; i++) {
				if (newQuery) {
					query = "INSERT  INTO BillEntryDetails ('billNo', 'qty','rateAmount',rateDescr,delivered) " 
					newQuery = false;
				}
				else {
					query += ' UNION';
				}
				var billId:String 		=  billEntryDetails[i].billNo;
				var qty:Number 			=  billEntryDetails[i].qty;
				var rateAmount:Number 	=  billEntryDetails[i].rateAmount;
				var rateDescr:String 	=  billEntryDetails[i].rateDescr;
				var delivered:String 	=  billEntryDetails[i].delivered;
				
				query += ' SELECT "' + billId +'", '+ qty +', ' + rateAmount +', "' + rateDescr + '","' + delivered +'"' ;
				
				if (iCnt!=0 && iCnt%499==0) {
					insertRecordsInDatabse(query);
					newQuery = true;
					iCnt = 0;
					query = "";
				}
				iCnt++;
			}		
			
			if ( iCnt % 499 !=0 ) {
				insertRecordsInDatabse(query);
			}
		}


		public function insertIntoBillPaymentDetails(data:Object) : void{

			var sql:String ="INSERT INTO BillPaymentDetails " +
				"(billNo,recdDate, recdAmount, balAmount, totAmount,custID )" +
				" VALUES (:col1, :col2, :col3, :col4, :col5, :col6)";
			insertRecordSQLStmt = new SQLStatement();
			insertRecordSQLStmt.sqlConnection = sqlConnection;
			insertRecordSQLStmt.text = sql;
			insertRecordSQLStmt.parameters[':col1'] = data.billNo;
			insertRecordSQLStmt.parameters[':col2'] = data.recdDate;
			insertRecordSQLStmt.parameters[':col3'] = data.recdAmount;
			insertRecordSQLStmt.parameters[':col4'] = data.balAmount;
			insertRecordSQLStmt.parameters[':col5'] = data.totAmount;
			insertRecordSQLStmt.parameters[':col6'] = data.custID;
			insertRecordSQLStmt.execute();
		}
		
		private function insertRecordsInDatabse(query:String):void{
			try{
				insertRecordSQLStmt = new SQLStatement();
				insertRecordSQLStmt.sqlConnection = sqlConnection;
				insertRecordSQLStmt.text = "";
				insertRecordSQLStmt.text = query;
				insertRecordSQLStmt.execute();
			}catch(e:Error){
				trace(e);
			}
		}		
		
		
			
		public function updateLoginMaster(data:Object):void{
			var sqlQueryString:String = "update loginMaster set loginPass ='" + data.loginPass + "', userType='" + data.userType +"' where loginName ='" + data.loginName +"'";
			updateRecordSQLStmt = new SQLStatement();
			updateRecordSQLStmt.sqlConnection = sqlConnection;
			updateRecordSQLStmt.text = sqlQueryString;
			updateRecordSQLStmt.execute(-1, new Responder( updateRecordResultHandler, selectRecordErrorHandler ) );
		}

		public function updateBillNoTable(ibillNoToUpdate:int):void{
			var ibillNo:int = ibillNoToUpdate -1;
			var sqlQueryString:String = "update BillNoTable set billNo ='" + ibillNoToUpdate.toString() + "' where billNo ='" + ibillNo.toString() +"'";
			updateRecordSQLStmt = new SQLStatement();
			updateRecordSQLStmt.sqlConnection = sqlConnection;
			updateRecordSQLStmt.text = sqlQueryString;
			updateRecordSQLStmt.execute(-1, new Responder( updateRecordResultHandler, selectRecordErrorHandler ) );
		}
		
		public function updateCustomerMaster(data:Object):void{
			var sqlQueryString:String = "update cutomerMaster set custName ='" + data.custName + "'," ;
			sqlQueryString += " custPhone='" 	+ data.custPhone +"',";
			sqlQueryString += " pantLength='" 	+ data.pantLength +"',";
			sqlQueryString += " pantW='" 		+ data.pantW +"',";
			sqlQueryString += " pantS='" 		+ data.pantS +"',";
			sqlQueryString += " pantB='" 		+ data.pantB +"',";
			sqlQueryString += " pantNL='" 		+ data.pantNL +"',";
			sqlQueryString += " pantNR='" 		+ data.pantNR +"',";
			sqlQueryString += " pantT='" 		+ data.pantT +"',";
			sqlQueryString += " pantF='" 		+ data.pantF +"',";
			sqlQueryString += " pantBelt='" 	+ data.pantBelt +"',";
			sqlQueryString += " pantFP='" 		+ data.pantFP +"',";
			sqlQueryString += " pantBP='" 		+ data.pantBP +"',";
			sqlQueryString += " pantWP='" 		+ data.pantWP +"',";
			sqlQueryString += " pantups='" 		+ data.pantups +"',";
			sqlQueryString += " pantF1='" 		+ data.pantF1 +"',";
			sqlQueryString += " pantB1='" 		+ data.pantB1 +"',";
			sqlQueryString += " shirtL='" 		+ data.shirtL +"',";
			sqlQueryString += " shirtSH='" 		+ data.shirtSH +"',";
			sqlQueryString += " shirtSL='" 		+ data.shirtSL +"',";
			sqlQueryString += " shirtSR='" 		+ data.shirtSR +"',";
			sqlQueryString += " shirtAL='" 		+ data.shirtAL +"',";
			sqlQueryString += " shirtC='" 		+ data.shirtC +"',";
			sqlQueryString += " shirtW='" 		+ data.shirtW +"',";
			sqlQueryString += " shirtS='" 		+ data.shirtS +"',";
			sqlQueryString += " shirtNeck='" 		+ data.shirtNeck +"',";
			sqlQueryString += " shirtPock='" 		+ data.shirtPock +"',";
			sqlQueryString += " shirtCollar='" 		+ data.shirtCollar +"'";
			
			sqlQueryString += " where custID ='" + data.custID +"'";
			updateRecordSQLStmt = new SQLStatement();
			updateRecordSQLStmt.sqlConnection = sqlConnection;
			updateRecordSQLStmt.text = sqlQueryString;
			updateRecordSQLStmt.execute(-1, new Responder( updateRecordResultHandler, selectRecordErrorHandler ) );
			
		}
		
		public function updateRateMaster(data:Object):void{
			var sqlQueryString:String = "update rateMaster set rateCode ='" + data.rateCode + "', rateDescr='" + data.rateDescr +"',"+ "amount = '"+  data.amount + "'" + " where rateCode ='" + data.rateCode +"'";
			updateRecordSQLStmt = new SQLStatement();
			updateRecordSQLStmt.sqlConnection = sqlConnection;
			updateRecordSQLStmt.text = sqlQueryString;
			updateRecordSQLStmt.execute(-1, new Responder( updateRecordResultHandler, selectRecordErrorHandler ) );
		}
		
		private function updateRecordResultHandler(res:SQLResult):void{
			trace("User profile updated");
		}
		
		public function deleteRecordsFromDatabase(sqlQueryString:String):void{
			deleteRecordSQLStmt = new SQLStatement();
			deleteRecordSQLStmt.sqlConnection = sqlConnection;
			deleteRecordSQLStmt.text = sqlQueryString;
			deleteRecordSQLStmt.execute(-1, new Responder( deleteRecordResultHandler, selectRecordErrorHandler ) );
		}
		private function deleteRecordResultHandler(res:SQLResult):void{
			;
		}

		public function selectQueryHandler(callBack:Function, sqlQueryString:String, searchKey:String = ""):void{
			callBackFunc = callBack;
			selectRecordSQLStmt = new SQLStatement();
			selectRecordSQLStmt.sqlConnection = sqlConnection;
			selectRecordSQLStmt.text = sqlQueryString;
			if(searchKey != ""){
				selectRecordSQLStmt.parameters[0] = '%' + searchKey + '%';
			}
			selectRecordSQLStmt.execute(-1, new Responder( selectRecordResultHandler, selectRecordErrorHandler) );
		}
		
		private function selectRecordErrorHandler(e:SQLErrorEvent):void{
			trace(e);
		}
		
		private function selectRecordResultHandler(res:SQLResult):void{
			selectRecordSQLStmt.clearParameters();
			selectRecordSQLStmt = null;
			var arr:ArrayCollection ;//= new Array();
			if ( res != null && res.data != null ) {
				arr = new ArrayCollection(res.data);
			}
			callBackFunc(arr);
		}
		
		
	}
}
class SingletonEnforcer{}